package com.cw.service.impl;

import com.cw.common.constant.Response;
import com.cw.dto.AppointmentDto;
import com.cw.dto.AppointmentWeekCountDto;
import com.cw.dto.DayConfigurationDto;
import com.cw.dto.WeekAppointmentDto;
import com.cw.mapper2.DayConfigurationMapper;
import com.cw.service.DayConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DayConfigurationServiceImpl implements DayConfigurationService {

    @Autowired
    private DayConfigurationMapper dayConfigurationMapper;
    @Override
    public Response saveBatch(DayConfigurationDto dayConfigurationDto) {
        Response checkResult = checkData(dayConfigurationDto);
        if(checkResult.getCode()!=200)
        {
            return checkResult;
        }

        for (int i = 0; i < dayConfigurationDto.getTimeSlotLists().size(); i++) {
            dayConfigurationDto.getTimeSlotLists().get(i).setSort(i);
        }

        // 将预约天数插入数据库表
        dayConfigurationMapper.deleteAppointmentDayCountByType(dayConfigurationDto.getType());
        dayConfigurationMapper.insertAppointmentDayCount(dayConfigurationDto.getAppointmentDayCount(), dayConfigurationDto.getType());

        // 插入数据库
        dayConfigurationMapper.deleteAppointmentTimeSlot(dayConfigurationDto.getType());
        dayConfigurationMapper.insertAppointmentTimeSlot(dayConfigurationDto.getTimeSlotLists(),dayConfigurationDto.getType());

        // 生成号源
        dayConfigurationMapper.deleteAppointmentNaturalTimeSlot(dayConfigurationDto.getType());
        for (int j = 1; j <= 7; j++) {
            dayConfigurationMapper.insertAppointmentNaturalTimeSlot(j,dayConfigurationDto.getTimeSlotLists(),dayConfigurationDto.getType());
        }
        return Response.success("保存成功");
    }

    @Override
    public Response updateBatch(List<WeekAppointmentDto> weekAppointmentDto,Integer type) {
        dayConfigurationMapper.deleteAppointmentNaturalTimeSlot(type);
        List<AppointmentWeekCountDto> insertlist = new ArrayList<>();
        weekAppointmentDto.stream().forEach(w->{
            w.getDayAndCountDtoList().forEach(d->{
                AppointmentWeekCountDto dto = new AppointmentWeekCountDto();
                dto.setSort(w.getSort());
                dto.setTimeSlot(w.getTimeSlot());
                dto.setType(type);
                dto.setWeekday(d.getWeekday());
                dto.setAppointmentCount(d.getAppointmentCount());
                insertlist.add(dto);
            });
        });
        dayConfigurationMapper.insertNewWeekCount(insertlist);
        return Response.success("更新成功");
    }

    @Override
    public Integer getMaxCount(String type) {
        return dayConfigurationMapper.selectBedMaxCount(type);
    }

    @Override
    public DayConfigurationDto getAllInfo(Integer type) {
        DayConfigurationDto dto = new DayConfigurationDto();
        // 查询天数
        Integer dayCount = dayConfigurationMapper.getDayCount(type);
        // 查询配置
        List<AppointmentDto> timeSlotConfigurationLists = dayConfigurationMapper.getTimeSlotConfiguration(type);
        // 查询号源
        List<WeekAppointmentDto> weekAppointmentDtoList = dayConfigurationMapper.getWeekInfo2(type);

        dto.setWeekAppointmentLists(weekAppointmentDtoList);
        dto.setAppointmentDayCount(dayCount);
        dto.setType(Integer.valueOf(type));
        dto.setTimeSlotConfigurationLists(timeSlotConfigurationLists);
        return dto;
    }

    public Response checkData(DayConfigurationDto dayConfigurationDto){
        if (dayConfigurationDto.getType() == null){
            return Response.error("没有区分病房和输注");
        }
        List<AppointmentDto> timeSlotList = dayConfigurationDto.getTimeSlotLists();
        boolean result = timeSlotList.stream().anyMatch(i -> (i.getTimeSlot() != null && i.getAppointmentCount()==null ||
                i.getAppointmentCount() != null && i.getTimeSlot() == null));
        if (result){
            return Response.error("请同时输入预约时间段和预约数量");
        }
        return  Response.success("success");
    }
}
